package com.ruoyi.biz.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.biz.domain.ReviewRequest;
import com.ruoyi.biz.mapper.BizSubmitWorkMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.biz.domain.BizWork;
import com.ruoyi.biz.service.IBizWorkService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 作业Controller
 * 
 * @author lhb
 * @date 2025-02-24
 */
@RestController
@RequestMapping("/biz/work")
public class BizWorkController extends BaseController
{
    @Resource
    private IBizWorkService bizWorkService;

    @Resource
    private BizSubmitWorkMapper bizSubmitWorkMapper;


    /*
    * 一键退回
    * */
    @DeleteMapping("/return/{rate}")
    public AjaxResult returnWork(@PathVariable Integer rate,Long workId ){
        BigDecimal decimal = new BigDecimal(rate);
        BigDecimal divide = decimal.divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
        // 将查重率大于divide的作业退回
        bizSubmitWorkMapper.deleteByWorkIdAndRate(divide,workId);
        return AjaxResult.success();
    }

    /*
    * 查重
    * */
    @PostMapping("/review")
    public AjaxResult review(@RequestBody ReviewRequest request) throws IOException {
        Boolean b = bizWorkService.checkWork(request.getIds(), request.getWorkId());
        return AjaxResult.success(b);
    }



    /**
     * 查询作业列表
     */
    @PreAuthorize("@ss.hasPermi('biz:work:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizWork bizWork)
    {
        startPage();
        List<BizWork> list = bizWorkService.selectBizWorkList(bizWork);
        return getDataTable(list);
    }

    /**
     * 导出作业列表
     */
    @PreAuthorize("@ss.hasPermi('biz:work:export')")
    @Log(title = "作业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizWork bizWork)
    {
        List<BizWork> list = bizWorkService.selectBizWorkList(bizWork);
        ExcelUtil<BizWork> util = new ExcelUtil<BizWork>(BizWork.class);
        util.exportExcel(response, list, "作业数据");
    }

    /**
     * 获取作业详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:work:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizWorkService.selectBizWorkById(id));
    }

    /**
     * 新增作业
     */
    @PreAuthorize("@ss.hasPermi('biz:work:add')")
    @Log(title = "作业", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizWork bizWork)
    {
        return toAjax(bizWorkService.insertBizWork(bizWork));
    }

    /**
     * 修改作业
     */
    @PreAuthorize("@ss.hasPermi('biz:work:edit')")
    @Log(title = "作业", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizWork bizWork)
    {
        return toAjax(bizWorkService.updateBizWork(bizWork));
    }

    /**
     * 删除作业
     */
    @PreAuthorize("@ss.hasPermi('biz:work:remove')")
    @Log(title = "作业", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bizWorkService.deleteBizWorkByIds(ids));
    }
}
